import { Anyaction } from "../../untils/index";
import { Toast } from "react-vant";
const infodata = {
  data: [],
  childrenData: [],
  userInfo: {},
};

const reducer = (state = infodata, { type, payload }: Anyaction) => {
  let newState = JSON.parse(JSON.stringify(state));
  const show_toast = (code: number, message: string) => {
    switch (code) {
      case 200:
        return Toast.success(message);
      case 500:
        return Toast.fail(message);

      default:
        break;
    }
  };
  switch (type) {
    case "GET_LIST":
      newState.data = payload;
      newState.childrenData = payload[0].children;
      return newState;
    case "CHECK_ACTIVE":
      newState.childrenData = newState.data[payload].children;
      return newState;
    case "LOGIN":
      if (payload.code == 200) {
        newState.userInfo = {
          username: payload.username,
          password: payload.password,
        };
        window.localStorage.setItem(
          "token",
          JSON.stringify({
            username: payload.username,
            password: payload.password,
          })
        );
      }
      show_toast(payload.code,payload.msg)
      newState.code=payload.code
      return newState;
      case "REG":
        if(payload.code===201){
          alert("已经注册过了")
        }else{
            alert("注册成功")
        }
        return newState;
    default:
      return newState;
  }
};
export default reducer;
